/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : BreadCrumbTag.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 19, 2011  mycodetalks			       Created
 */

package net.lwf.web.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import net.lwf.entity.WebUser;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IWebConstant;
import net.lwf.web.struts.action.UserDataHelper;

import org.apache.commons.lang.StringUtils;


public class BreadCrumbTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String contextPath;

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		this.contextPath = request.getContextPath();
		try {
			pageContext.getOut().print(this.generateBc(request));
		} catch (Exception ex) {
			throw new JspTagException("SimpleTag: " + ex.getMessage());
		}
		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}
	
	private String generateBc(HttpServletRequest request) {
		WebUser webuser = UserDataHelper.getWebUser(request);
		if(webuser != null && webuser.getUser() != null) {
			String grade = webuser.getUser().getGrade();
			Object subObj = WebUtil.getAttribute(request, IWebConstant.SUBJECT);
			Object topObj = WebUtil.getAttribute(request, IWebConstant.TOPIC);

			if(StringUtils.isEmpty(grade) && subObj != null && topObj != null) {
				return "";
			}
			return this.getBcTable(grade, subObj, topObj);
		}

		return "";

	}
	
	private String getBcTable(String grade, Object subject, Object topic) {
		String subvalue = (subject == null) ? null : subject.toString();
		String topvalue = (topic == null) ? null : topic.toString();
		StringBuilder table = new StringBuilder();
		//table.append("<div class=\"breadcrumb\" >");
			table.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"breadcrumb\" >");
				table.append("<tr>");
				table.append("<td width=\"40%\" >&nbsp;</td>");
				table.append("<td width=\"1%\" >&nbsp;</td>");	
					table.append("<td width=\"15%\" nowrap>");
						table.append("Grade: <b>" + this.getGradeName(grade) + "</b>");
					table.append("</td>");
						if(subvalue!= null) {
							table.append("<td width=\"1%\" >&nbsp;</td>");
							table.append("<td width=\"3%\" >");
							table.append("<img src=\"" + this.contextPath + "/image/arrows/arrowright2.png" + "\" />");
							table.append("</td>");
							table.append("<td width=\"8%\" >");
								table.append("Subject: ");
								table.append("</td>");
								table.append("<td width=\"10%\" align=\"left\" nowrap>");
								table.append(this.getLink(subvalue, subvalue, null));
								table.append("</td>");
						}
						else {
							table.append("<td width=\"22%\" >&nbsp;</td>");
						}
						if(topvalue != null) {
							table.append("<td width=\"1%\" >&nbsp;</td>");
							table.append("<td width=\"3%\" >");
							table.append("<img src=\"" + this.contextPath + "/image/arrows/arrowright2.png" + "\" />");
							table.append("</td>");
							table.append("<td width=\"8%\" >");
								table.append("Topic: ");
								table.append("</td>");
								table.append("<td width=\"10%\" align=\"left\" nowrap>");
								table.append(this.getLink(topvalue, subvalue, topvalue));
								table.append("</td>");
						}
						else {
							table.append("<td width=\"22%\" >&nbsp;</td>");
						}
						
				table.append("</tr>");
			table.append("</table>");	
		//table.append("</div>");
		return table.toString();

	}
	
	private String getGradeName(String grade) {
		if(grade != null) {
			int nGrade = Integer.parseInt(grade);
			switch(nGrade) {
				case  0: return "Kinder Garten";
				case -1: return "Pre K";
				case  1: return "1st";
				case  2: return "2nd";
				case  3: return "3rd";
				case  4: return "4th";
				case  5: return "5th";
				case  6: return "6th";
				default: return "Unknown";
			}
		}
		return "Unknown";
	}
/*	private String generateBc(HttpServletRequest request) {
		WebUser webuser = (WebUser) WebUtil.getAttribute(request, IWebConstant.WEB_USER);
		String grade = webuser.getUser().getGrade();
		String subject = WebUtil.getOptionalParameter(request, "subject");
		String topic = WebUtil.getOptionalParameter(request, "topic");
		if(subject == null && topic == null) {
			return "";
		}
		String[] param = new String[2];
		String[] paramvalue = new String[2];
		
		StringBuilder table = new StringBuilder("");
		table.append("<table border=\"0\"" + ">");
			table.append("<tr>");
				table.append("<td colspan=\"3\" class=\"ss\""   + ">");
				table.append(grade);
				table.append("</td>");
			table.append("</tr>");	
			table.append("<tr>");
				table.append("<td class=\"ss\""   + ">");
				table.append("&nbsp;");
				table.append("</td>");
				table.append("<td colspan=\"2\" class=\"ss\""   + ">");
				param[0] = IWebConstant.SUBJECT;
				paramvalue[0] = subject;
				table.append(this.getLink(request, subject, param, paramvalue));				
				table.append("</td>");				
			table.append("</tr>");	
			if(topic != null) {
				table.append("<tr>");
					table.append("<td colspan=\"2\" class=\"ss\""   + ">");
					table.append("&nbsp;");
					table.append("</td>");
					table.append("<td class=\"ss\""   + ">");
					param[1] = IWebConstant.TOPIC;
					paramvalue[1] = topic;
					table.append(this.getLink(request, topic, param, paramvalue));					
					table.append("</td>");				
				table.append("</tr>");					
			}
			table.append("</table>");
		return table.toString();
	}*/
	
/*	private String generateBc(HttpServletRequest request) {
		WebUser webuser = (WebUser) WebUtil.getAttribute(request, IWebConstant.WEB_USER);
		String grade = webuser.getUser().getGrade();
		String subject = request.getParameter("subject");
		String topic = request.getParameter("topic");
		if(subject == null && topic == null) {
			return "";
		}
		String[] param = new String[2];
		String[] paramvalue = new String[2];
		
		StringBuilder table = new StringBuilder("");
		table.append("<table border=\"0\"" + ">");
			table.append("<tr>");
				table.append("<td class=\"ss\"" + ">");
				table.append(grade);
				table.append("</td>");
				
				table.append("<td class=\"ss\"" + ">");
				table.append(" - ");
				table.append("</td>");	
				
				table.append("<td class=\"ss\"" + ">");
				param[0] = IWebConstant.SUBJECT;
				paramvalue[0] = subject;
				table.append(this.getLink(request, subject, param, paramvalue));
				
				if(topic != null) {
					table.append("<td class=\"ss\"" + ">");
					table.append(" - ");
					table.append("</td>");	
					
					table.append("<td class=\"ss\"" + ">");
					param[1] = IWebConstant.TOPIC;
					paramvalue[1] = topic;
					table.append(this.getLink(request, topic, param, paramvalue));				
				}
				
					table.append("</td>");					
				table.append("</tr>");
			table.append("</table>");
			System.out.println("============> html string: " + table.toString());
		return table.toString();
	}*/
	
	//private String getSubjectLink()

/*	private String getLink(HttpServletRequest request, String linkname, String[] param, String[] paramvalue) {
		String path = request.getContextPath() + "/tabmenu.do?action=show";
		int nCounter = 0;
		for(String parameter: param) {
			if (parameter != null) {
				path += "&" + parameter + "=" + paramvalue[nCounter];
			}
			
			nCounter++;
		}
		StringBuilder link = new StringBuilder("<a href=\"" + path + "\" class=\"bluelink\"" + ">");
		link.append(linkname);
		link.append("</a>");
		return link.toString();
	}*/
	
	private String getLink(String param, String subject, String topic) {
		String target = this.contextPath + "/sitemap.do?action=display";
		if(subject != null) {
			target += "&subject=" + subject;
		}
		if(topic != null) {
			target += "&topic=" + topic;
		}
		StringBuilder link = new StringBuilder("<a href=\"" + target + "\"" + ">");
		link.append(param);
		link.append("</a>");
		return link.toString();
	}

} //BreadCrumbTag
